//
//  RFDemoRDeliveryLogger.m
//  RFIXiOSDemo
//
//  Created by bill bai on 2025/2/8.
//

#import "RFDemoRDeliveryLogger.h"

// ⚠️ ！！！！ 注意 ！！！！
// 请将本文件 54 行和 89 的 NSLog 修改为你自己的日志输出方式

@interface RFDemoRDeliveryLogger ()
@property(nonatomic, assign) RAFTLogLevel currentLogLevel;
@end

@implementation RFDemoRDeliveryLogger

+ (instancetype)sharedInstance {
  static dispatch_once_t onceToken;
  static RFDemoRDeliveryLogger *instance = nil;
  dispatch_once(&onceToken, ^{
    instance = [[RFDemoRDeliveryLogger alloc] init];
    instance.currentLogLevel = RAFTLogLevelAll;
  });
  return instance;
}

/*! @brief 记日志
 *
 *  @param tag 自定义tag
 *  [level][time][][tag][file, func, line][msg
 */
- (void)log:(RAFTLogLevel)level
        tag:(NSString *)tag
       file:(const char *)file
       func:(const char *)func
       line:(int)line
        msg:(NSString *)msg, ... NS_REQUIRES_NIL_TERMINATION {
  if (level < self.currentLogLevel || !msg) {
    return;
  }
  
  va_list args;
  va_start(args, msg);
  
  NSString *message = @"";
  if (va_arg(args, id)) {
    @try {
      message = [[NSString alloc] initWithFormat:msg arguments:args];
    } @catch (NSException *exception) {
      // ⚠️
      // !!!!!!! 请注意：这里的日志输出方式，只是示例，实际使用时，请根据自己的需求，替换为自
      NSLog(@"[RDeliveryLog] log param error:%@", exception);
    }
  } else {
    message = msg;
  }
  va_end(args);
  
  NSString *levelStr = nil;
  switch (level) {
    case RAFTLogLevelVerbose:
      levelStr = @"[RDeliveryLog][Verbose]";
      break;
    case RAFTLogLevelDebug:
      levelStr = @"[RDeliveryLog][Debug]";
      break;
    case RAFTLogLevelInfo:
      levelStr = @"[RDeliveryLog][Info]";
      break;
    case RAFTLogLevelWarn:
      levelStr = @"[RDeliveryLog][Warn]";
      break;
    case RAFTLogLevelError:
      levelStr = @"[RDeliveryLog][Error❌]";
      break;
    case RAFTLogLevelFatal:
      levelStr = @"[RDeliveryLog][Fatal]";
      break;
    case RAFTLogLevelNone:
      levelStr = @"";
      break;
    default:
      break;
  }
  // ⚠️
  // !!!!!!! 请注意：这里的日志输出方式，只是示例，实际使用时，请根据自己的需求，替换为自己的日志输出方式。
  NSLog(@"%@ %@ | %s:%d | %s | %@", levelStr, tag, file, line, func, message);
}

/*! @brief 记日志
 *
 *  @param tags 多tag数组
 *  [level][time][][tag][file, func, line][msg
 */
- (void)log:(RAFTLogLevel)level
       tags:(NSArray<NSString *> *)tags
       file:(const char *)file
       func:(const char *)func
       line:(int)line
        msg:(NSString *)msg, ... NS_REQUIRES_NIL_TERMINATION {
  if (level < self.currentLogLevel || !msg) {
    return;
  }
  
  NSMutableString *tagStr = [NSMutableString string];
  for (NSString *tag in tags) {
    [tagStr appendString:tag];
  }
  
  va_list args;
  va_start(args, msg);
  
  NSString *message = @"";
  if (va_arg(args, id)) {
    @try {
      message = [[NSString alloc] initWithFormat:msg arguments:args];
    } @catch (NSException *exception) {
      NSLog(@"[RDeliveryLog] log param error:%@", exception);
    }
  } else {
    message = msg;
  }
  va_end(args);
  
  [self log:level tag:tagStr file:file func:func line:line msg:message, nil];
}

/*! @brief 设置打印log的级别
 *  @param level level
 */
- (void)setLogLevel:(RAFTLogLevel)level {
  self.currentLogLevel = level;
}

/*! @brief 强制写入日志到文件
 *
 */
- (void)flushLog {
  //默认实现不写文件
}

@end
